﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace SimpleTiles.Classes
{
    public class TileManager
    {
        public static void createTile(Note nota)
        {
            String name = nota.Titolo;
            /*var ctl = new DynamicTile(nota.Titolo);
            ctl.Measure(new Size(173, 173));
            ctl.Arrange(new Rect(0, 0, 173, 173));
            var bmp = new WriteableBitmap(173, 173);
            bmp.Render(ctl, null);
            bmp.Invalidate();*/

            var iss = IsolatedStorageFile.GetUserStoreForApplication();
            var filename = "/Shared/ShellContent/note" + nota.Titolo + ".jpg";
            var imgname = "/Shared/ShellContent/" + nota.Titolo + ".jpg";
     
            /*using (var stm = iss.CreateFile(filename))
            {
                bmp.SaveJpeg(stm, 173, 173, 0, 80);
            }*/
           
            // pin 
            var data = new StandardTileData
            {
                //Title = name,
                //BackTitle = "backTitle",
                //BackContent = "back content",

                BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute),
                BackBackgroundImage = new Uri("isostore:" + imgname, UriKind.Absolute)
            };
            ShellTile.Create(new Uri("/NotePage.xaml?titolo=" + nota.Titolo, UriKind.Relative), data);
        }
        public static void updateTile(Note nota)
        {
            String name = nota.Titolo;
            /*var ctl = new DynamicTile(nota.Titolo);
            ctl.Measure(new Size(173, 173));
            ctl.Arrange(new Rect(0, 0, 173, 173));
            var bmp = new WriteableBitmap(173, 173);
            bmp.Render(ctl, null);
            bmp.Invalidate();*/

            var iss = IsolatedStorageFile.GetUserStoreForApplication();
            var filename = "/Shared/ShellContent/note" + nota.Titolo + ".jpg";
            var imgname = "/Shared/ShellContent/" + nota.Titolo + ".jpg";

            /*using (var stm = iss.CreateFile(filename))
            {
                bmp.SaveJpeg(stm, 173, 173, 0, 80);
            }*/

            // pin 
            var data = new StandardTileData
            {
                //Title = name,
                //BackTitle = "backTitle",
                //BackContent = "back content",

                BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute),
                BackBackgroundImage = new Uri("isostore:" + imgname, UriKind.Absolute)
            };
            
            IEnumerable<ShellTile> activeTiles = ShellTile.ActiveTiles;
            ShellTile TileToFind = activeTiles.FirstOrDefault(x => x.NavigationUri.ToString().EndsWith("titolo=" + nota.Titolo));
            if (TileToFind != null)
            {
                TileToFind.Update(data);
            }
        }
        public static bool ExistsTile(Note nota)
        {
            IEnumerable<ShellTile> activeTiles = ShellTile.ActiveTiles;
            ShellTile TileToFind = activeTiles.FirstOrDefault(x => x.NavigationUri.ToString().EndsWith("titolo=" + nota.Titolo));

            if (TileToFind != null)
            {
                return true;
            }
            return false;
        }

        public static void DeleteTile(Note nota)
        {
            IEnumerable<ShellTile> activeTiles = ShellTile.ActiveTiles;
            ShellTile TileToFind = activeTiles.FirstOrDefault(x => x.NavigationUri.ToString().EndsWith("titolo=" + nota.Titolo));

            if (TileToFind != null)
            {
                TileToFind.Delete();
            }
        }

        public static void WriteBitmap(WriteableBitmap image, String name)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                image.SaveJpeg(stream, (int)image.PixelWidth, (int)image.PixelHeight, 0, 100);

                using (var local = new IsolatedStorageFileStream("/Shared/ShellContent/"+name+".jpg", FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication()))
                {
                    local.Write(stream.GetBuffer(), 0, stream.GetBuffer().Length);
                }
            }
        }
    }
}
